package com.camera.yl.widght

import android.content.Context
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.DrawableRes
import com.camera.yl.R

class GridItemView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {

    private val imageView: ImageView
    private val textView: TextView

    init {
        // 设置背景和其他属性
        background = context.getDrawable(R.drawable.grid_item_background)
        gravity = android.view.Gravity.CENTER
        orientation = VERTICAL
        
        // 加载内部视图
        LayoutInflater.from(context).inflate(R.layout.view_grid_item, this, true)
        
        // 获取子视图引用
        imageView = findViewById(R.id.grid_item_image)
        textView = findViewById(R.id.grid_item_text)
        
        // 获取自定义属性
        if (attrs != null) {
            val typedArray = context.obtainStyledAttributes(attrs, R.styleable.GridItemView)
            try {
                val icon = typedArray.getResourceId(R.styleable.GridItemView_itemIcon, 0)
                if (icon != 0) {
                    imageView.setImageResource(icon)
                }
                
                val text = typedArray.getString(R.styleable.GridItemView_itemText)
                if (text != null) {
                    textView.text = text
                }
            } finally {
                typedArray.recycle()
            }
        }
    }
    
    /**
     * 设置图标资源
     */
    fun setIcon(@DrawableRes iconRes: Int) {
        imageView.setImageResource(iconRes)
    }
    
    fun setIconDrawable(iconRes: Drawable) {
        imageView.setImageDrawable(iconRes)
    }
    
    /**
     * 设置文本内容
     */
    fun setText(text: String) {
        textView.text = text
    }
} 